package resources;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.Option;
import org.eclipse.californium.core.server.resources.CoapExchange;

import java.util.List;

/**
 * describe:
 *
 * @author helloworldyu
 * @data 2017/12/8
 */
public class BaseResource extends CoapResource{
	/**
	 * 自定义的 option 编号，代表 TOKEN。
	 */
	public static final int CUSTOM_TOKEN_NUMBER=61;

	public BaseResource(String name) {
		super(name);
	}


	/**
	 * 获取 token
	 *
	 * @param exchange
	 * @return
	 */
	protected static String getToken( CoapExchange exchange ){
		List<Option> others = exchange.getRequestOptions().getOthers();

		//查找 61 号属性。
		for (Option option : others ){
			if ( option.getNumber() == CUSTOM_TOKEN_NUMBER ){
				return option.getStringValue();
			}
		}

		return null;
	}
}